package framework;

import static org.testng.AssertJUnit.assertTrue;

import java.io.BufferedReader;
import java.io.InputStreamReader;



public abstract class BaseEntity{
	protected static Logger logger = Logger.getInstance();
//	private boolean wasScreenShoted = false;
	protected static JAssert jassert = JAssert.getInstance();
	protected static Browser browser = Browser.getInstance();
	protected static boolean isLogged = false;
	protected static int screenIndex = 0;
	private static final String PROPERTIES_FILE = "selenium.properties";
	protected static final String CH_PROJECT_PREFIX = "Izm";
	//Логины
	protected static final String LOGIN_PART1 = "loginPart1"; //223-223-223
	protected static final String LOGIN_PART2 = "loginPart2"; //65
	protected static final String PASSWORD = "password";
	protected static final String LOGIN = "login";
	protected static final String LOGIN_PART_NOT_REGISTER = "loginPart_unreg_org";
	protected static final String LOGIN_PART_UNREGISTER_USER = "loginPart_unreg_user";
	protected static final String LOGIN_UNREG_ORG = "login_unreg_org";
	protected static final String LOGIN_UNREGISTER_USER = "login_unregister_user";
	// Логин и пароль для формы загрузки xml
	protected static final String LOGIN_UPLOAD_PART1 = "loginUploadPart1";
	protected static final String LOGIN_UPLOAD_PART2 = "loginUploadPart2";
	protected static final String LOGIN_UPLOAD = "loginUpload";
	protected static final String UPLOAD_PASSWORD = "uploadPassword";
	// Для входа в личный кабинет как загружающий XML
	protected static final String LOGIN_LOADER_PART1 = "loginLoaderPart1";
	protected static final String LOGIN_LOADER_PART2 = "loginLoaderPart2";
	protected static final String LOGIN_LOADER = "loginLoader";
	protected static final String PASS_LOADER = "passwordLoader";

	
	protected static PropertiesResourceManager props = new PropertiesResourceManager(PROPERTIES_FILE);
	protected abstract String formatLogMsg(String message);

	protected void debug(String message) {
		logger.debug(String.format("[%1$s] %2$s", this.getClass().getSimpleName(), formatLogMsg(message)));
	}

	protected void info(String message) {
		logger.info(formatLogMsg(message));
	}

	protected void warn(String message) {
		logger.warn(formatLogMsg(message));
	}

	protected void error(String message) {
		logger.error(formatLogMsg(message));
	}

	protected void fatal(String message) {
		logger.fatal(formatLogMsg(message));
		assertTrue(formatLogMsg(message), false);
	}
	
	/**
	 * killing process by Image name
	 */
	public void checkAndKill() {
		logger.info("killing processes");
		try {
			String line;
			Process p = Runtime.getRuntime().exec(String.format("taskkill /IM %1$s.exe /F",browser.currentBrowser.toString()));
			BufferedReader input =
					new BufferedReader(new InputStreamReader(p.getInputStream()));
			while (((line = input.readLine()) != null)) {
				logger.info(line);
			}
			input.close();
		} catch (Exception err) {
			err.printStackTrace();
		}
	}
}

